package com.ruix.app.widget.clip;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * User: Ray
 * Date: 15/10/10
 * ReadMe: 图片裁剪－移动缩放－自定义view－圆形裁剪框
 */
public class ClipRoundImageBorderView extends View {
    /**
     * 水平方向与View的边距
     */
    private int mHorizontalPadding;
    /**
     * 边框的宽度 单位dp
     */
    private int mBorderWidth = 1;

    private Paint mPaint;//圆环（空心圆）paint
    private Paint mPaintCirle1;//圆（实心圆）paint-1
    private Paint mPaintCirle2;//圆（实心圆）paint-2
    private Paint mPaintRect;//阴影层paint

    private Canvas mCanvas;//阴影层画布
    private RectF mRect;//整个屏幕

    private Bitmap mBgBitmap;//用于绘制阴影层的bitmap

    public ClipRoundImageBorderView(Context context) {
        this(context, null);
    }

    public ClipRoundImageBorderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClipRoundImageBorderView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mBorderWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mBorderWidth, getResources().getDisplayMetrics());

        //绘制阴影层
        mPaintRect = new Paint();
        mPaintRect.setColor(Color.parseColor("#30000000"));
//        mPaintRect.setARGB(185, 0, 0, 0);

        // 绘制实心圆-1
        mPaintCirle1 = new Paint();
        mPaintCirle1.setStrokeWidth((getWidth() - 2 * mHorizontalPadding) / 2);  //实心圆半径
        mPaintCirle1.setARGB(255, 0, 0, 0);


        // 绘制实心圆-2
        mPaintCirle2 = new Paint();
        mPaintCirle2.setStrokeWidth((getWidth() - 2 * mHorizontalPadding) / 2);  //实心圆半径
        mPaintCirle2.setARGB(255, 0, 0, 0);
        mPaintCirle2.setXfermode(new PorterDuffXfermode(Mode.XOR));//XOR模式：重叠部分被掏空

        // 绘制圆环
        mPaint = new Paint();
        mPaint.setStyle(Style.STROKE);
        mPaint.setAntiAlias(true); //抗锯齿
        mPaint.setColor(Color.RED);//边框颜色白色
        mPaint.setStrokeWidth(mBorderWidth);//画笔宽度


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mBgBitmap == null) {
            mBgBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
            mCanvas = new Canvas(mBgBitmap);
            mRect = new RectF(0, 0, getWidth(), getHeight());
        }
        //绘制阴影层
        mCanvas.drawRect(mRect, mPaintRect);
        //绘制实心圆 ，绘制完后，在mCanvas画布中，mPaintRect和mPaintCirle相交部分即被掏空
        mCanvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - mHorizontalPadding, mPaintCirle1);
        mCanvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - mHorizontalPadding, mPaintCirle2);
        //将阴影层画进本View的画布中
        canvas.drawBitmap(mBgBitmap, null, mRect, new Paint());
        //绘制圆环
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - mHorizontalPadding, mPaint);

    }

    public void setHorizontalPadding(int mHorizontalPadding) {
        this.mHorizontalPadding = mHorizontalPadding;

    }

}
